//
//  GKZYCustomPushAnimation.m
//  GKZY_CLASS
//
//  Created by KwokChun Cheung on 15/12/3.
//  Copyright © 2015年 eenet. All rights reserved.
//

#import "GKZYCustomPushAnimation.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface GKZYCustomPushAnimation()<UIViewControllerAnimatedTransitioning>

@end

@implementation GKZYCustomPushAnimation

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.4;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    //目的ViewController
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    //起始ViewController
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    //添加toView到上下文
    [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];
    
    
    
    //自定义动画
    CGRect mainRect = [[UIScreen mainScreen] bounds];
    toViewController.view.transform = CGAffineTransformMakeTranslation(mainRect.size.width, 0);
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        
        fromViewController.view.transform = CGAffineTransformMakeTranslation(-mainRect.size.width, 0);
        
        toViewController.view.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
        fromViewController.view.transform = CGAffineTransformIdentity;
        
        // 声明过渡结束时调用 completeTransition: 这个方法
        
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        
    }];
    
}

@end
